
import React, { Component, PropTypes } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import DataEditTable from '../../addons/DataEditTable'
import * as menuService from '../../../services/menus';
import { deepEqual } from '../../../utils/objects';
import * as indiDiviService from '../../../services/AccountDivi';

const daoName = "COMP_ACCO_ACBL";
const ButtonGroup = Button.Group;
const columns = [
  { title: '日期', width: 100, sorter: true, dataIndex: 'DTID', key: 'DTID', dispaly: true },
  { title: '余额', width: 100, sorter: true, dataIndex: 'ACBL', key: 'ACBL', dispaly: true },
];
const controls = [
  { name: "DTID", label: "日期", type: "text", grid: 12 / 24, required: true },
  { name: "ACBL", label: "余额", type: "number", grid: 12 / 24, required: true },
]
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
class COMP_ACCO_ACBL extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false,
      searchData: {},
      title: "",
      showEdit: false,
      isOK: 0,
      diviDataSource: []
    };
  }
  componentWillReceiveProps(nextProps) {
    if ((!deepEqual(nextProps.jlid, this.props.jlid))) {
      this.loadData(nextProps.jlid, this.props.odsDate)
      this.state.acid = nextProps.jlid
    }
  }
  // componentDidMount() {
  //   this.loadData(this.props.jlid, this.props.odsDate)
  // }
  loadData(jlid, odsDate) {
    let d = odsDate.substring(0, 10).replace("-", "").replace("-", "")
    let seeks = [];
    let whereItems = [];
    whereItems.push({
      FieldName: "ACID",
      Operator: "=",
      FieldValues: '' + jlid
    }, {
        FieldName: "DTID",
        Operator: "=",
        FieldValues: '' + d
      }, {
        FieldName: "1",
        Operator: "<>",
        FieldValues: "1"
      })
    seeks.push({
      WhereItems: whereItems,
      RelationSign: "AND"
    })
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, pageSize: 99999999, seeks }
    });
  }
  hideEditHandler() {
    this.props.dispatch({
      type: `${this.props.parentModel}/hideEdit`
    });
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }
  findData() {
    let ksq = '';
    let qzr = '';
    let acid = this.props.jlid;
    const form = this.refs.AccoForm.getForm();
    form.validateFields((err, values) => {
      if (!err) {
        ksq = values.KSRQ;
        qzr = values.JSRQ
      }
    });
    if (ksq === null) {
      message.error("开始日期不能为空！")
      return
    } else {
      ksq = datetime.format(ksq, "yyyy-MM-dd").replace("-", "").replace("-", "")
    }
    if (qzr === null) {
      message.error("结束日期不能为空！")
      return
    } else {
      qzr = datetime.format(qzr, "yyyy-MM-dd").replace("-", "").replace("-", "")
    }
    let seeks = [];
    let whereItems = [];
    whereItems.push({
      FieldName: "DTID",
      Operator: ">=",
      FieldValues: ksq
    }, {
        FieldName: "DTID",
        Operator: "<=",
        FieldValues: qzr
      }, {
        FieldName: "ACID",
        Operator: "=",
        FieldValues: '' + acid
      })
    seeks.push({
      WhereItems: whereItems,
      RelationSign: "AND"
    })
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, pageSize: 99999999, seeks }
    });
  }
  render() {
    let { list, odsDate } = this.props
    list = this.state.diviDataSource.length <= 0 ? list : this.state.diviDataSource;
    const controlsFilter = [
      {
        name: "KSRQ", label: "开始日期", type: "date", grid: 7 / 24, required: false,
        dataType: "date", layout: { labelSpan: 10, wrapperSpan: 14 }
      }, {
        name: "JSRQ", label: "结束日期", type: "date", grid: 7 / 24, required: false,
        dataType: "date", layout: { labelSpan: 10, wrapperSpan: 14 }
      }, {
        name: 'CX', required: false, type: 'button-group',
        data: [{ value: '1', label: '查询', onClick: this.findData.bind(this) }],
        grid: 10 / 24, layout: { labelSpan: 14, wrapperSpan: 11 }
      },
    ];
    return (
      <Modal title={this.props.title} width={750} visible={this.props.showEdit} onCancel={this.hideEditHandler.bind(this)} closable={true} footer={null}>
        <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
          <div>
            <div className={styles.create}>
              <EditFrom ref="AccoForm" daoName={daoName} controls={controlsFilter} clearItemMargin={true} selectFirst={false} />
            </div>
            <DataEditTable ref="table" columns={columns} controls={controls} footer={true} dataSource={list} />
          </div>
        </div>
      </Modal>
    )
  }

}

COMP_ACCO_ACBL.propTypes = {
  jlid: PropTypes.number,
  dtid: PropTypes.number,
  showEdit: PropTypes.bool,
  title: PropTypes.string,
  parentModel: PropTypes.string


};
COMP_ACCO_ACBL.defaultProps = {
  jlid: -1,
  dtid: -1,
  showEdit: false,
  title: "",
  parentModel: ""
};

function mapStateToProps(state) {
  const { list } = state[daoName];
  const { odsDate } = state.menus;
  return {
    list, odsDate
  }
}
COMP_ACCO_ACBL = connect(mapStateToProps)(COMP_ACCO_ACBL)
export default COMP_ACCO_ACBL;
